<div class="container-fluid">
    <h4 class="page-title">学生</h4>
    <!--表格-->
    <div class="row" style="margin-bottom: 2%">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header" style="margin-top: 1%">
                    <!--<button id="batchDeletion" class="btn btn-default btn-sm"
                            style="float: right;margin-left: 1%">批量删除
                    </button>-->
                    <button onclick="resetForm()" class="btn btn-default btn-sm" style="float: right;"
                            data-toggle="modal" data-target="#addModel">新增
                    </button>
                    <!--搜索-->
                    <form class="navbar-left navbar-form nav-search mr-md-3" action="" style="float: right">
                        <div class="input-group">
                            <input id="search" type="text" placeholder="姓名..." class="form-control">
                            <div class="input-group-append">
								<span class="input-group-text">
									<i id="searchBtn" class="la la-search search-icon"></i>
								</span>
                            </div>
                        </div>
                    </form>
                    <h4 class="card-title">学生列表</h4>
                </div>
                <div class="card-body">
                    <table id="table" class="table table-hover">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var token;
    $(function () {
        // debugger
        token = sessionStorage.getItem('token')
        var oTable = new TableInit();
        oTable.Init();
        //getPower()
        //dialogueMessage()
        //audit();
        roleSelect();
    });
    var TableInit = function () {
        var oTableInit = new Object();
        oTableInit.Init = function () {
            //初始化Table
            //alert(1);
            $('#table').bootstrapTable({
                url: '/oa/t-student/listStudent?token='+token,
                type: "GET",
                sidePagination: "client",
                pagination: true,
                pageNumber: 1,
                pageSize: 5,
                pageList: [5, 10, 25, 50, 100],
                height: 350,
                formatNoMatches: function(){
                    return "没有相关的匹配结果";
                },
                formatLoadingMessage: function(){
                    return "请稍等，正在加载中。。。";
                },
                responseHandler:function(res){
                    if (res.code == 401) {
                        window.location.href = 'login.html'
                    }else {
                        for (var i = 0 ;i < res.data.length ; i++) {
                            if (res.data[i].sex == 0) {
                                res.data[i].sex = "女"
                            }else{
                                res.data[i].sex = "男"
                            }
                        }
                    }
                    return res;
                },
                columns: [{
                    checkbox: true,
                    visible: true
                }, {
                    field: 'studNo',
                    title: '学号',
                    align: 'center'
                }, {
                    field: 'name',
                    title: '姓名',
                    align: 'center'
                }, {
                    field: 'age',
                    title: '年龄',
                    align: 'center'
                }, {
                    field: 'sex',
                    title: '性别',
                    align: 'center'
                }, {
                    field: 'term',
                    title: '班级',
                    align: 'center'
                }, {
                    field: 'major',
                    title: '专业',
                    align: 'center'
                }, {
                    field: 'createTime',
                    title: '创建时间',
                    align: 'center'
                }, {
                    field: 'operate',
                    title: '操作',
                    align: 'center',
                    events: operateEvents,
                    formatter: operateFormatter //自定义方法，添加操作按钮
                }]
            });
        }
        return oTableInit;
    };
    function operateFormatter(value, row, index) {//赋予的参数
        if (row.roleId == "管理员"){
            return [
                '<button class="editUser btn btn-default btn-xs" style="margin: 0 5px 0 5px")">编辑</button>',
                '<button disabled="disabled" class="resetPass btn btn-default btn-xs" style="margin: 0 5px 0 5px")">重置密码</button>',
            ].join('');
        }
        return [
            '<button class="editStudent btn btn-default btn-xs" style="margin: 0 5px 0 5px")">编辑</button>',
            '<button class="deleteUser btn btn-default btn-xs" style="margin: 0 5px 0 5px">删除</button>',
        ].join('');
    }
    window.operateEvents = {
        //启用或停用
        'click .enableOrDisable': function (e, value, row, index) {
            var param = {
                userId:row.id
            }
            $.ajax({
                url: '/oa/t-user/updateState',
                type: "POST",
                data: param,
                dataType: "json",
                headers: {
                    token: "" + token
                },
                success: function (data) {
                    if (data.code == 200){
                        $.message(data.msg);
                    }else if (data.code == 401) {
                        window.location.href = 'login.html'
                    }else {
                        $.message({
                            message:data.msg,
                            type:'error'
                        });
                    }
                    $('#table').bootstrapTable('refresh');
                }
            })
        },
        'click .editStudent': function (e, value, row, index) {
            $('#addModel').modal('show');
            console.log(row);
            /*$.ajax({
                url: '/oa/t-role/listRole?token='+token,
                type: "GET",
                dataType: "json",
                success: function (data) {
                    if (data.code == 200){
                        var data = data.data
                        $('#roleId').empty();
                        var html = "";
                        html += '<option value="">请选择角色...</option>\n'
                        for (var i = 0; i < data.length; i++) {
                            if (row.roleId == data[i].roleName){
                                html += '<option value="'+data[i].roleId+'" selected="selected">'+data[i].roleName+'</option>\n'
                            }else{
                                html += '<option value="'+data[i].roleId+'">'+data[i].roleName+'</option>\n'
                            }
                        }
                        $('#roleId').append(html)
                    }else if (data.code == 401) {
                        window.location.href = 'login.html'
                    }else {
                        $.message({
                            message:data.msg,
                            type:'error'
                        });
                    }
                }
            })*/
            $('#studNo').val(row.studNo),
                $('#name').val(row.name),
                $('#age').val(row.age),
                $('#sex').val(row.sex),
                $('#term').val(row.term),
                $('#major').val(row.major),
                $('#addBtn').unbind("click").on('click',function () {
                    if ($('#name').val().replace(/\s+/g, "") == "") {
                        $.message({
                            message:'请输入姓名',
                            type:'warning'
                        });
                        return;
                    }

                    var param = {
                        id:row.id,
                        studNo:$('#studNo').val(),
                        name:$('#name').val(),
                        age:$('#age').val(),
                        sex:$('#sex').val(),
                        term: $('#term').val(),
                        major: $('#major').val()
                    }
                    $.ajax({
                        url: '/oa/t-student/updateStudent',
                        type: "POST",
                        data: JSON.stringify(param),
                        dataType: "json",
                        headers: {
                            token: "" + token
                        },
                        contentType: 'application/json; charset=UTF-8',
                        success: function (data) {
                            if (data.code == 200){
                                $.message(data.msg);
                            }else if (data.code == 401) {
                                window.location.href = 'login.html'
                            }else {
                                $.message({
                                    message:data.msg,
                                    type:'error'
                                });
                            }
                            $('#table').bootstrapTable('refresh');
                            resetForm()
                            $('#addModel').modal('hide')
                        }
                    })
                })
        },
        'click .resetPass': function (e, value, row, index) {
            var param = {
                userId:row.id
            }
            $.ajax({
                url: '/oa/t-user/rePass',
                type: "POST",
                data: param,
                dataType: "json",
                headers: {
                    token: "" + token
                },
                success: function (data) {
                    if (data.code == 200){
                        $.message(data.msg);
                    }else if (data.code == 401) {
                        window.location.href = 'login.html'
                    }else {
                        $.message({
                            message:data.msg,
                            type:'error'
                        });
                    }
                    $('#table').bootstrapTable('refresh');
                }
            })
        },
        'click .deleteUser': function (e, value, row, index) {
            $('#deleteModel').modal('show')
            $('#deleteBtn').unbind("click").on('click',function () {
                var param = {
                    studentId:row.id
                }
                $.ajax({
                    url: '/oa/t-student/deleteStudent',
                    type: "POST",
                    data: param,
                    dataType: "json",
                    headers: {
                        token: "" + token
                    },
                    success: function (data) {
                        if (data.code == 200){
                            $.message(data.msg);
                        }else if (data.code == 401) {
                            window.location.href = 'login.html'
                        }else {
                            $.message({
                                message:data.msg,
                                type:'error'
                            });
                        }
                        $('#table').bootstrapTable('refresh');
                        $('#deleteModel').modal('hide')
                    }
                })
            })
        },
    };
    $('#addBtn').on('click',function () {
        if ($('#studNo').val().replace(/\s+/g, "") == "") {
            $.message({
                message:'请输入学号',
                type:'warning'
            });
            return;
        }
        if ($('#name').val().replace(/\s+/g, "") == "") {
            $.message({
                message:'请输入姓名',
                type:'warning'
            });
            return;
        }

        var param = {
            studNo: $('#studNo').val(),
            name:$('#name').val(),
            age:$('#age').val(),
            sex:$('#sex').val(),
            term: $('#term').val(),
            major: $('#major').val()
        }
        $.ajax({
            url: '/oa/t-student/saveStudent',
            type: "POST",
            data: JSON.stringify(param),
            dataType: "json",
            headers: {
                token: "" + token
            },
            contentType: 'application/json; charset=UTF-8',
            success: function (data) {
                if (data.code == 200){
                    $.message(data.msg);
                }else if (data.code == 401) {
                    window.location.href = 'login.html'
                }else {
                    $.message({
                        message:data.msg,
                        type:'error'
                    });
                }
                $('#table').bootstrapTable('refresh');
                resetForm()
                $('#addModel').modal('hide')
            }
        })
    })
    $('#batchDeletion').on('click',function () {
        var row = $('#table').bootstrapTable('getSelections')
        var str = ""
        for (var i = 0 ; i < row.length ; i++) {
            if (row[i].roleId != "管理员"){
                str += row[i].id+','
            }
        }
        $('#deleteModel').modal('show')
        $('#deleteBtn').unbind("click").on('click',function () {
            var param = {
                userId:str
            }
            $.ajax({
                url: '/oa/t-user/deleteListUser',
                type: "POST",
                data: param,
                dataType: "json",
                headers: {
                    token: "" + token
                },
                success: function (data) {
                    if (data.code == 200){
                        $.message(data.msg);
                    }else if (data.code == 401) {
                        window.location.href = 'login.html'
                    }else {
                        $.message({
                            message:data.msg,
                            type:'error'
                        });
                    }
                    $('#table').bootstrapTable('refresh');
                    $('#deleteModel').modal('hide')
                }
            })
        })
    })
    function roleSelect() {
        $.ajax({
            url: '/oa/t-role/listRole?token='+token,
            type: "GET",
            dataType: "json",
            success: function (data) {
                if (data.code == 200){
                    var data = data.data
                    $('#roleId').empty();
                    var html = "";
                    html += '<option value="">请选择角色...</option>\n'
                    for (var i = 0; i < data.length; i++) {
                        html += '<option value="'+data[i].roleId+'">'+data[i].roleName+'</option>\n'
                    }
                    $('#roleId').append(html)
                }else if (data.code == 401) {
                    window.location.href = 'login.html'
                }else {
                    $.message({
                        message:data.msg,
                        type:'error'
                    });
                }
            }
        })
    }
    function resetForm() {
        $('#userName').val(''),
            $('#roleId').val(''),
            $('#userEmail').val(''),
            $('#userPhone').val('')
    }
    $("#searchBtn").unbind('click').on('click',function () {
        var keyword = $('#search').val();
        $('#table').bootstrapTable('refresh', {
            query:
                {
                    studentName: keyword
                }
        });
    })
</script>